<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:01
 */

namespace HiCommon\Model;

class Coupons extends Base
{
    
    protected $table = 'coupons';
    
    protected $guarded = [];
    
    //public $timestamps = false;
    
    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];
    
  /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value)
        {
            case 0:
                $res = ['id'=>0,'name'=>'草稿'];
                break;
            case 1:
                $res = ['id'=>1,'name'=>'待审核'];
                break;
            case 2:
                $res = ['id'=>2,'name'=>'通过'];
                break;
            case 3:
                $res = ['id'=>3,'name'=>'拒绝'];
                break;
            default:
                $res = ['id'=>-1,'name'=>'未知'];
        }
        return $res;
    }
    
        /**
         * 获取图片可用链接
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getImgUrlAttribute($value)
        {
            $arr_data = [
              'src'=>$value,
              'full_src'=>oss_full_url($value),
            ];
            return $arr_data;
        }
        
        /**
         * 扩展信息
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getExtendAttribute()
        {
            $s_created_at = $this->created_at;
            $arr_data = [
              'created_at'=>ymd_friendly_date($s_created_at),
            ];

            return $arr_data;
        }

        public function countNum(){
            return $this->hasMany(CouponGrantDetail::class,'id','coupon_id');
        }

        //商品详情统计是否领取
        public function counttogoods(){
            $where['user_id'] = api_user_id();   //当前登录用户
            $where['status'] = 1;    //未使用 （领取过但是已经使用或者过期后还可以在领取）
            $where['type'] = 3;      //手动领取的券  与其他方式获得的券不冲突
            return $this->belongsTo(CouponGrantDetail::class,'id','coupon_id')->where( $where )->select('coupon_id');
        }


    /**
     *  关联商户信息
     *  hinq 2019 09 11
     *
     **/
    public function shop(){
        return $this->belongsTo(Shop::class,'repairstore_id')->select(['name','id','logo']);
    }

    public function count_ling(){
        return $this->hasMany(CouponGrantDetail::class,'coupon_id');
    }
    public function count_yong(){
        return $this->hasMany(CouponGrantDetail::class,'coupon_id')->where('status', 2);
    }
    public function count_guo(){
        return $this->hasMany(CouponGrantDetail::class,'coupon_id')->where('status' ,3);
    }


}